package com.test.platform.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.test.platform.model.DeveloperInfo
import com.test.platform.repository.DeveloperInfoRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class DeveloperInfoViewModel(
    private val repository: DeveloperInfoRepository
) : ViewModel() {
    private val _developerInfos = MutableStateFlow<List<DeveloperInfo>>(emptyList())
    val developerInfos: StateFlow<List<DeveloperInfo>> = _developerInfos.asStateFlow()
    
    private val _loading = MutableStateFlow(false)
    val loading: StateFlow<Boolean> = _loading.asStateFlow()
    
    private val _error = MutableStateFlow<String?>(null)
    val error: StateFlow<String?> = _error.asStateFlow()
    
    fun loadDeveloperInfos() {
        viewModelScope.launch(Dispatchers.IO) {
            _loading.value = true
            try {
                val result = repository.getAllDeveloperInfos()
                withContext(Dispatchers.Main) {
                    _developerInfos.value = result
                    _error.value = null
                }
            } catch (e: Exception) {
                withContext(Dispatchers.Main) {
                    _error.value = e.message ?: "加载失败"
                }
            } finally {
                withContext(Dispatchers.Main) {
                    _loading.value = false
                }
            }
        }
    }

    fun createDeveloperInfo(
        owner: String,
        name: String,
        developer: String
    ) {
        viewModelScope.launch(Dispatchers.IO) {
            try {
                val developerInfo = DeveloperInfo(
                    owner = owner,
                    name = name,
                    developer = developer
                )
                repository.createDeveloperInfo(developerInfo)
                loadDeveloperInfos()
            } catch (e: Exception) {
                withContext(Dispatchers.Main) {
                    _error.value = e.message ?: "创建失败"
                }
            }
        }
    }

    fun updateDeveloperInfo(id: Int, developerInfo: DeveloperInfo) {
        viewModelScope.launch(Dispatchers.IO) {
            try {
                repository.updateDeveloperInfo(id, developerInfo)
                loadDeveloperInfos()
            } catch (e: Exception) {
                withContext(Dispatchers.Main) {
                    _error.value = e.message ?: "更新失败"
                }
            }
        }
    }

    fun deleteDeveloperInfo(id: Int) {
        viewModelScope.launch(Dispatchers.IO) {
            try {
                repository.deleteDeveloperInfo(id)
                loadDeveloperInfos()
            } catch (e: Exception) {
                withContext(Dispatchers.Main) {
                    _error.value = e.message ?: "删除失败"
                }
            }
        }
    }
}